﻿IF EXISTS (SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME = 'OTEntregaSave')
	BEGIN
		DROP  Procedure  OTEntregaSave
	END
GO


CREATE PROCEDURE OTEntregaSave 	
	(			
		@Id						AS INT,		
		@ManoObraIndirecta		AS DECIMAL(10,2) = NULL,
		@UnidadesProducidas		AS DECIMAL(10,2) = NULL
	)	
AS
BEGIN	
	SET NOCOUNT ON		
	
	-- Remueve todos registros asociados para recrearlos despues en los otros SPs					
	DELETE FROM Lit_OTDetalle	WHERE OTEntregaId = @id
	DELETE FROM Lit_OTMaterial	WHERE OTEntregaId = @id
	DELETE FROM Lit_OTGasto		WHERE OTEntregaId  = @id
		
	UPDATE Lit_OTEntrega
	SET ManoObraIndirecta = @ManoObraIndirecta,
		UnidadesProducidas = @UnidadesProducidas,
		EstadoId = 2,  -- En Proceso	
		FechaInicial = ISNULL(FechaInicial, GETDATE())
	WHERE Id = @Id
			
	SELECT @Id
				
	SET NOCOUNT OFF
END
GO  